package com.lxy.digitaltwins.common.task;


import com.lxy.digitaltwins.domain.vo.ThermometerVO;
import com.lxy.digitaltwins.properties.IoTDBProperties;
import com.lxy.digitaltwins.service.EquipmentService;
import org.apache.iotdb.isession.SessionDataSet;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.session.Session;
import org.apache.tsfile.read.common.RowRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Component
public class DataTask {

    private static final double MIN_VALUE = 30.0;
    private static final double MAX_VALUE = 50.0;
    private static final double MAX_CHANGE = 0.5;
    private static final int DECIMAL_PLACES = 2; // 保留2位小数
    private double currentValue;
    private final Random random = new Random();

    @Autowired
    private  IoTDBProperties ioTDBProperties;

    @Autowired
    private EquipmentService equipmentService;

    //伪装数据源，每五秒向IoTDB发送假数据

    @Scheduled(cron = "0/5 * * * * ?")
    public void addDate() throws Exception {
        Session session=new Session.Builder()
                .host(ioTDBProperties.getHost())
                .port(ioTDBProperties.getPort())
                .username(ioTDBProperties.getUserName())
                .password(ioTDBProperties.getPassWord())
                .build();
            session.open(false);
            session.setFetchSize(10000);
        List<String> names=new ArrayList<>();
        //获取传感器名称
        String sql1="show timeseries root.transformerSubstation.*.*";
        SessionDataSet dataSet = session.executeQueryStatement(sql1);
        //封装数据
        while(dataSet.hasNext()) {
            RowRecord record = dataSet.next();
            String name = record.getFields().get(0).getStringValue();
            names.add(name);
        }
        dataSet.close();
        for (String name : names) {
            //分割数据
            //分割设备和传感器
            String device="";
            String sensor="";
            int lastDotIndex = name.lastIndexOf('.');
            // 截取最后一部分（点之后的内容）
            sensor= name.substring(lastDotIndex + 1);
            // 截取点之前的所有内容
            device = name.substring(0, lastDotIndex);
            //准备数据
            SensorDataGenerator generator = new SensorDataGenerator();
            long currentTimeMillis = System.currentTimeMillis();
            ArrayList<String> meaus=new ArrayList<>();
            meaus.add(sensor);
            ArrayList<String> values=new ArrayList<>();
            values.add(Double.toString(generator.getNextValue()));
            //插入数据
            session.insertRecord(device,currentTimeMillis,meaus,values);
            //TODO 测试用
            System.out.println(name+ "插入数据："+values);
        }
        System.out.println("-----------------------------");
    }

    //测试用：接收数据
//    @Scheduled(cron = "0/5 * * * * ?")
//    public void reviceDate() throws Exception {
//        ThermometerVO thermometer = equipmentService.getThermometer();
//        System.out.println("接收数据："+thermometer.getTemp());
//
//    }



}
